package com.lht.cozyCloud.gateway.config;

import com.lht.cozyCloud.gateway.handler.SentinelFallbackHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * 网关限流配置
 * 
 * @author ruoyi
 */
@Configuration
public class GatewayConfig
{
    @Bean
    @Order(Ordered.HIGHEST_PRECEDENCE)  // 发生限流异常时,优先执行自定义限流异常处理
    public SentinelFallbackHandler sentinelGatewayExceptionHandler()
    {
        return new SentinelFallbackHandler();
    }

    @Bean
    public CorsWebFilter corsWebFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true); // 允许凭证请求
        config.addAllowedOriginPattern("*"); // 允许任何域进行跨域请求
//        config.addAllowedOrigin("http://192.168.31.38"); // 允许任何域进行跨域请求
        config.addAllowedHeader("*"); // 允许任何头进行跨域请求
        config.addAllowedMethod("*"); // 允许任何方法进行跨域请求
        source.registerCorsConfiguration("/**", config);
        return new CorsWebFilter(source);
    }
}